<?php
/**
 * user
 */
require_once 'lib/Cookie.php';
require_once 'lib/Mailer.php';
require_once 'lib/CException.php';
require_once 'services/UserService.php';
require_once 'services/IdCountService.php';

class UserController extends Controller{

	public function index(){
		/**/
	}

	public function register(){
		include parent::getRender();
	}

	/**
	 * 激活账号页面
	 */
	public function active(){
		$user_id = parent::getRequest()->getParam('uid');
		$sign    = parent::getRequest()->getParam('s');
		if( empty($user_id) && empty( $sign ) ){
			echo '错误信息';
		}else{
			$userService = new UserService();
			$user = $userService->getUserById( $user_id );
			/* 是否已经激活 */
			if($user['status']){
				echo '你已经激活了';
				exit;
			}
			$user = $userService->getUserById( $user_id );
			$sign2 = md5( $user['password'].$user['ip'].$user['load_time'] );
			if( empty($user) || $sign2 != $sign ){
				echo '错误信息';
			}else{
				include parent::getRender();
			}
		}

	}

	/**
	 * 激活账户 && 完成注册
	 */
	public function useable(){
		if( !parent::getRequest()->isPost() ){
			throw new CException("访问错误", 404);
		}

		$user_id = parent::getRequest()->getParam('uid');
		$sign    = parent::getRequest()->getParam('s');
		$domain  = parent::getRequest()->getParam('domain');
		$nickname = parent::getRequest()->getParam('nickname');

		if( empty( $user_id ) || empty( $sign ) || empty( $domain ) || empty( $nickname ) ){
			echo '信息没有填写完全';exit;
		}

		/* domain 需要验证 */
		$domain = strtolower( $domain );
		$match  = preg_match("/^[a-z]{4,}$/", $domain); 
		if(!$match){
			echo 'domain只限英文';exit;
		}

		$userService = new UserService();
		$user = $userService->getUserById( $user_id );
		/* 是否已经激活 */
		if($user['status']){
			echo '你已经激活了';
			exit;
		}

		/* 签名验证 */
		$sign2 = md5( $user['password'].$user['ip'].$user['load_time'] );
		if( $sign2 != $sign ){
			echo '错误信息';
			exit;
		};

		
		/*domain 是否被使用*/
		$domainExists = $userService->doaminExists($domain);
		if($domainExists){
				echo 'domain is used';exit;
		}

		/* 注册完成 */
		$ret = $userService->activeUser( $domain, $nickname, $user_id );
		if($ret){
			echo 'success';
		}else{
			echo 'Failed';
		}
		exit;
	}

	/**
	 * 登录
	 */
	public function login(){
		//login save
		if( parent::getRequest()->getParam('email') && parent::getRequest()->getParam('password') ){
			if( !parent::getRequest()->isPost() ){
				echo 'error';	
				exit;
			}

			$email = parent::getRequest()->getParam('email');
			$password = md5( md5( parent::getRequest()->getParam('password') ).parent::$_config['application']['salt']);
			$userService = new UserService();
			$user_id = $userService->emailExists( $email );

			if( !$user_id ){
				echo 'email not exists, please register';
				exit;
			}else{
				$user = $userService->getUserById($user_id);
				if( $user['password'] != $password ){
					echo 'password error';
					exit;
				}
				else if( $user['status'] != 1 ){
					echo '账号没有激活';
					exit;
				}else{
					$userService->setUserCookie( $email, $user['password'] );
					header("Location:/");
					exit;
				}
			}

		}else{
			//login view
			include parent::getRender();
		}
	}

	/**
	 * 创建账号
	 */
	public function create(){
		if(!parent::getRequest()->isPost()){
			echo 'not post method';	
			exit;
		}

		$options = array();
		$options['email'] = parent::getRequest()->getParam('email');
		$options['password'] = md5( md5( parent::getRequest()->getParam('password') ).parent::$_config['application']['salt']);

		$userService = new UserService( parent::$_config );

		if($userService->emailExists( $options['email'] )){
			echo 'user is exists';exit;
		}

		$idCountService = new IDCountService();
		$user_id = $idCountService->generateId();

		$options['uid'] = $user_id;
		$options['ip'] = parent::getRequest()->getIp();
		// inactive
		$options['status'] = 0;
		$options['load_time'] = time();
		
		if( $userService->createUser( $user_id, $options ) ){
			$sign = md5( $options['password'].$options['ip'].$options['load_time'] );
			$activeUrl = "http://127.0.0.1/user/active?uid={$user_id}&s={$sign}";
			$mail = new Mailer();
			$mailTemplate = file_get_contents('mailTemp/active.html', true);
			$mailTemplate = str_replace("{url}", $activeUrl, $mailTemplate);
			$mail->send( array($options['email']=>$options['email']),"请激活乌龙岛账号", $mailTemplate );
			echo 'create success, please active your account';exit;
		}else{
			echo 'create user err';exit;
		}

	}
}
?>
